package org.neu.ccs.secureim;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.security.KeyPair;
import java.security.SecureRandom;
import java.util.StringTokenizer;

import org.apache.commons.codec.binary.Base64;

public class GenerateUsersFile {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try
		{
			BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream("Users.in")));
			FileOutputStream w = new FileOutputStream("Users.conf", true);
			String input = r.readLine();

			SecureRandom rand = new SecureRandom();
			
			while( input != null && !input.trim().isEmpty() )
			{
				StringTokenizer t = new StringTokenizer(input);
				String username = t.nextToken();
				String password = t.nextToken();
				
				System.out.println("Creating account for " + username + " with password " + password);
				
				byte[] salt = new byte[128];
				rand.nextBytes(salt);
				
				KeyPair pair = CryptoHelper.GenerateClientKeyPair(username, password.toCharArray(), salt);
				w.write(username.getBytes());
				w.write(new String(" ").getBytes());
				w.write(Base64.encodeBase64(salt));
				w.write(new String(" ").getBytes());
				w.write(Base64.encodeBase64(pair.getPublic().getEncoded()));
				
				w.write(new String("\n").getBytes());
				
				input = r.readLine();
			}
			
			r.close();
			w.close();
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}
